<template>
  <div class="login">
    <el-form class="form" :model="form" :rules="rules" ref="form">
      <el-form-item>
        <img src="@/assets/common/login-logo.png" alt="" />
      </el-form-item>
      <!-- 手机号输入框 -->
      <el-form-item prop="mobile">
        <el-input
          prefix-icon="el-icon-user-solid"
          v-model="form.mobile"
          @keyup.enter.native="submit"
        ></el-input>
      </el-form-item>
      <!-- 密码输入框 -->
      <el-form-item prop="password">
        <el-input
          prop="password"
          prefix-icon="el-icon-lock"
          show-password
          v-model="form.password"
          @keyup.enter.native="submit"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button class="btn" @click="submit">登录</el-button>
      </el-form-item>
      <el-form-item>
        <div class="tip">用户名：13800000002 密码：123456</div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
// import { sysLogin } from '@/api/user.js'
export default {
  data () {
    return {
      form: {
        mobile: '13800000002',
        password: '123456'
      },
      // 验证规则
      rules: {
        mobile: [
          { required: true, message: '必填', trigger: 'change' },
          {
            validator: (rules, value, callback) => {
              const reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              reg.test(value)
                ? callback()
                : callback(new Error('请输入正确的手机号'))
            },
            trigger: 'change'
          }
        ],
        password: [
          { required: true, message: '必填', trigger: 'change' },
          {
            min: 6,
            max: 12,
            message: '密码长度必须是6-12位数',
            trigger: 'change'
          }
        ]
      }
    }
  },
  methods: {
    submit () {
      // 全局表单验证
      this.$refs.form.validate(async results => {
        // results:true, 说明验证成功
        // results:false, 说明验证失败
        // console.log(results)
        // 验证成功，调用登录接口
        if (results) {
          // const res = await sysLogin(this.form)
          // this.$store.commit('user/setToken', res.data)
          // console.log(res)
          this.$store.dispatch('user/toLogin', this.form)
          this.$message.success('登录成功')
          this.$router.push('/')
        }
      })
    }
  }
}
</script>

<style lang="scss">
// scoped看得到的就管得到，看不到的就管不到
// 子组件只能看到最外层
// 希望管到内部   /deep/   ::v-deep
.login {
  width: 100%;
  height: 100%;
  background: url('~@/assets/common/login.jpg');
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;
  .btn {
    width: 100%;
    height: 60px;
    font-size: 24px;
    background-color: #407ffe;
    color: #fff;
    border: none;
  }
  .tip {
    text-align: center;
    color: #fff;
  }
  .el-input__inner {
    height: 50px;
  }
}
</style>
